<?php 
declare(strict_types=1);

namespace App\Service;

use App\Utils\UploadUtil;
use Hyperf\PhoneSearch\PhoneSearch;
use App\Exception\BusinessException;
use Hyperf\Di\Annotation\Inject;
use App\Constants\ErrorCode;
use App\Utils\ServerUtil;

class IndexApiService
 { 

    /**
     * @Inject 
     * @var PhoneSearch
     */
    protected $phoneInfo;


    //解析电话号码信息
    public function phone($phone){
        $info = $this->phoneInfo->get($phone);
        ServerUtil::info('通过号码获取号码信息',['path'=>'/api/index_mobile_operator','parameter'=>['phone'=>$phone],'result'=>$info]);
        $arr = explode("|", $info);
        if (count($arr) < 6) {
            throw new BusinessException(ErrorCode::ERR_BUESSUS,'无法判断号码类型');
        } else {
            $data['phoneArea'] = $arr[0];
            $data['phone'] = $phone;
            $data['phoneType'] = $arr[5];
            return $data;
        }
    }

    /** 远程图片下载上传接口
     * @Author abell
     * @DateTime 2020-05-28
     * @param string $request
     * @return array
     */
    public function downloadImg($url){
        $result = make(UploadUtil::class)->download_img($url);
        if($result['code']){
            return ['code'=>ErrorCode::ERR_SERVER,'msg'=>ErrorCode::getMessage(ErrorCode::ERR_SERVER),'data'=>''];
        }else{
            return ['code'=>ErrorCode::SUCCESS,'msg'=>ErrorCode::getMessage(ErrorCode::SUCCESS),'data'=>$result['imgpath']];
        }
    }

    /** 图片上传插件
     * @Author abell
     * @DateTime 2020-05-28
     * @param string $request
     * @return array
     */
    public function uploadImg($file){
        $result = make(UploadUtil::class)->upload_img($file);
        if($result){
            return ['code'=>ErrorCode::SUCCESS,'msg'=>ErrorCode::getMessage(ErrorCode::SUCCESS),'data'=>$result];
        }else{
            return ['code'=>ErrorCode::ERR_SERVER,'msg'=>ErrorCode::getMessage(ErrorCode::ERR_SERVER),'data'=>''];
        }
    }
 }